package com.hyx.api.service.impl.pc_admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hyx.api.domain.User;
import com.hyx.api.domain.Wallet;
import com.hyx.api.domain.WalletChangeInfo;
import com.hyx.api.mapper.UserMapper;
import com.hyx.api.mapper.WalletChangeInfoMapper;
import com.hyx.api.mapper.WalletMapper;
import com.hyx.api.service.pc_admin.IWalletService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@RequiredArgsConstructor
@Service
public class WalletServiceImpl implements IWalletService {

    private final WalletMapper walletMapper;

    private final UserMapper userMapper;

    private final WalletChangeInfoMapper walletChangeInfoMapper;

    @Override
    public Wallet selectWalletById(Long userId) {
        LambdaQueryWrapper<Wallet> lqw = Wrappers.lambdaQuery();
        lqw.eq(Wallet::getUserId, userId);
        return walletMapper.selectOne(lqw);
    }

    @Override
    public Wallet selectWalletByCompanyOrRecovery(Long companyId, Long recoveryId) {
        LambdaQueryWrapper<Wallet> lqw = Wrappers.lambdaQuery();
        if (companyId != null && companyId > 0) {
            lqw.eq(Wallet::getCompanyId, companyId);
        }
        if (recoveryId != null && recoveryId > 0) {
            lqw.eq(Wallet::getRecoveryBusId, recoveryId);
        }
        return walletMapper.selectOne(lqw);
    }

    @Override
    public int insertWallet(Wallet bo) {
        User user = userMapper.selectById(bo.getUserId());
        if (ObjectUtils.isEmpty(user))
            throw new RuntimeException("请输入正确的用户id");
        Wallet wallet = selectWalletById(user.getId());
        if (ObjectUtils.isNotEmpty(wallet))
            throw new RuntimeException("不可重复创建钱包");

        bo.setMoney(new BigDecimal(0));
        bo.setRecoveryBusId(user.getRecoveryId());
        bo.setCompanyId(user.getCompanyId());
        bo.setType(user.getType());
        return walletMapper.insert(bo);
    }

    @Transactional
    @Override
    public int addMoney(BigDecimal money, Long id, String name) {
        return setMoney(money, id, name, 1);
    }

    @Transactional
    @Override
    public int subMoney(BigDecimal money, Long id, String name) {
        return setMoney(money, id, name, 2);
    }


    public int setMoney(BigDecimal money, Long id, String name, Integer type) {
        WalletChangeInfo log = new WalletChangeInfo();
        log.setMoney(money);
        log.setStatus(2);//通过
        log.setType(type);
        log.setWalletId(id);
        log.setCreateBy(name);
        if (type == 1) {
            walletMapper.addMoney(money, id);
        } else if (type == 2) {
            walletMapper.subMoney(money, id);
        } else {
            return 0;
        }
        return walletChangeInfoMapper.insert(log);
    }
}
